//
//  FBtCorer.h
//  JwPack
//
//  Created by 陈警卫 on 2025/7/21.
//

#import <Foundation/Foundation.h>

#import "FBtConfig.h"
#import "FBtModel.h"

NS_ASSUME_NONNULL_BEGIN

@interface FBtCorer : NSObject

@property (nonatomic, assign) FBtState state;

/// Core配置
@property (nonatomic, strong) FBtConfig *config;

/// 外设数组
/// 数组中仅只有 (FBtPeriStateScaning/FBtPeriStateConnected/FBtPeriStateNotifyed) 状态的FBtPeriModel
@property (nonatomic, strong, readonly) NSMutableArray<FBtPeriModel *> *periModels;

// 回调

@property (nonatomic, copy) void(^didState)(FBtState state);
@property (nonatomic, copy) void(^didScanedPeri)(FBtPeriModel *periModel);
@property (nonatomic, copy) void(^didConncetedPeri)(FBtPeriModel *periModel);
@property (nonatomic, copy) void(^didConncetFailPeri)(FBtPeriModel *periModel);
@property (nonatomic, copy) void(^didDisconncetPeri)(FBtPeriModel *periModel);
@property (nonatomic, copy) void(^didNotifedPeri)(FBtPeriModel *periModel);
@property (nonatomic, copy) void(^didNotifedValue)(FBtPeriModel *periModel, NSData *data);
@property (nonatomic, copy) void(^didRssiPeri)(FBtPeriModel *periModel);

// 方法

/// 初始化
- (instancetype)initWithConfig:(FBtConfig *)config;

/// 添加扫描自动连接外设
/// connectUuid/connectName 必须其一
- (void)addScanConnectPeriModel:(FBtPeriModel *)periModel;

/// 扫描
- (void)scan;

/// 停止扫描
- (void)stopScan;

/// 连接
- (void)connectPeriModel:(FBtPeriModel *)periModel;

/// 断开连接
- (void)disconnectPeriModel:(FBtPeriModel *)periModel;

/// 通过外设UUID恢复
- (CBPeripheral *)retrievePeripheralsWithUuid:(NSString *)uuid;

/// 自动订阅外设
/// 自动添加 periModels 外设数组
/// FBtPeriStateDefault 增加扫描过滤
/// FBtPeriStateScaning 扫描自动连接
/// retrieve默认NO 为YES时 会通过恢复连接 恢复不到重新扫描
/// 此方法包含连接超时 通过 addScanConnectPeriModel/scan 方式需要自己设置启动超时判定 可以调用 [periModel startTimer] 接收didTimerAction超时回调
- (void)notifyPeriModel:(FBtPeriModel *)periModel comple:(void(^)(NSError *error))comple;


/// 获取已订阅的外设
- (NSArray<FBtPeriModel *> *)notifedPeriModel;

@end

NS_ASSUME_NONNULL_END
